package com.mtiiot.server.netty;

import java.util.concurrent.TimeUnit;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.mtiiot.util.Constants;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.timeout.IdleStateHandler;

public class ChildChannelHandler extends ChannelInitializer<SocketChannel> {
	private static Logger logger = LogManager.getLogger(ChildChannelHandler.class);
	@Override
	protected void initChannel(SocketChannel sc) throws Exception {
		logger.info("the server start a connection with one client");
//		sc.pipeline().addLast("frameDecoder", new ByteArrayDecoder());
//		sc.pipeline().addLast("frameEncoder", new ByteArrayEncoder());
//		sc.pipeline().addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
//		sc.pipeline().addLast("protobufDecoder", new ProtobufDecoder(InsData.Group.getDefaultInstance()));
//		sc.pipeline().addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
//		sc.pipeline().addLast("protobufEncoder", new ProtobufEncoder());
		
		sc.pipeline().addLast("ping", new IdleStateHandler(0, 0, Constants.config.getAllIdleTime(),TimeUnit.SECONDS));
		ByteBuf delimiter=Unpooled.copiedBuffer(Constants.config.getSeparator().getBytes());
		sc.pipeline().addLast(new DelimiterBasedFrameDecoder(Constants.config.getMsgMaxlenth(), delimiter));
		sc.pipeline().addLast(new TimeServerHandler());
	}
}
